package com.cineplanet.dew.dao;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cineplanet.dew.exception.DAOExcepcion;
import com.cineplanet.dew.modelo.Cine;
import com.cineplanet.dew.modelo.Pelicula;

public class PeliculaDAOImpl extends HibernateDaoSupport  implements PeliculaDAO{
	
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	@Override
	public List<Pelicula> listarPaisOrigen() throws DAOExcepcion {
		
		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		try {
			arreglo = getHibernateTemplate().find("select distinct paisOrigen from Pelicula");
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@Override
	public List<Pelicula> listarPublicoObjetivo() throws DAOExcepcion {
		
		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		try {
			arreglo = getHibernateTemplate().find("select distinct publicoObjetivo from Pelicula");
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@Override
	public Pelicula obtenerDetallesPelicula(int idPelicula) throws DAOExcepcion {
		
		Pelicula cine = new Pelicula();
		try {
			cine = (Pelicula)getHibernateTemplate().find("from Pelicula where cartelera = 1 " +
														 "and id_pelicula = ?",
													 	 String.valueOf(idPelicula)).get(0);
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return cine;
	}
	
	@Override
	public Pelicula obtenerDetalleCualquierPelicula(int idPelicula) throws DAOExcepcion {
		
		Pelicula cine = new Pelicula();
		try {
			cine = (Pelicula)getHibernateTemplate().find("from Pelicula where " +
														 " id_pelicula = ?",
													 	 String.valueOf(idPelicula)).get(0);
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return cine;
	}

	@Override
	public List<Pelicula> listar() throws DAOExcepcion {
		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		
		try {
			arreglo = getHibernateTemplate().find("from Pelicula");
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@Override
	public List<Pelicula> listarEnCartelera() throws DAOExcepcion {
		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		
		try {
			arreglo = getHibernateTemplate().find("from Pelicula where cartelera = 1");
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@Override
	public List<Pelicula> listarProximoEstreno() throws DAOExcepcion {
		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		
		try {
			arreglo = getHibernateTemplate().find("from Pelicula where cartelera = 0");
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Pelicula> listarPeliculasPorCine(int idCine)
			throws DAOExcepcion {

		List<Pelicula> arreglo = new ArrayList<Pelicula>();
		
		try {
			arreglo =  getHibernateTemplate().find("select pe from Pelicula pe, PeliculasPorCine pc " +
								"where pe.idPelicula = pc.pelicula.idPelicula " +
								"and pc.cine.idCine = ?", idCine);
						
				
			
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return arreglo;
	}

	@Override
	public Pelicula insertar(Pelicula pelicula) throws DAOExcepcion {
		try {
			getHibernateTemplate().save(pelicula);
		    getHibernateTemplate().flush();
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return pelicula;
	}

	@Override
	public Pelicula modificar(Pelicula pelicula) throws DAOExcepcion {
		try {
			getHibernateTemplate().update(pelicula);
		    getHibernateTemplate().flush();
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return pelicula;
	}

	@Override
	public Pelicula eliminar(Pelicula pelicula) throws DAOExcepcion {
		try {
			getHibernateTemplate().delete(pelicula);
		    getHibernateTemplate().flush();
		} catch (Exception e) {

			throw new DAOExcepcion(e.getMessage());
		}
		return pelicula;
	}
	


}
